package com.video.service.webcon.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.video.entity.Webcon;
import com.video.mapper.WebconMapper;
import com.video.service.task.ITaskService;
import com.video.service.webcon.ISolutionService;
import com.video.service.webcon.IWebconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Service
public class WebconServiceImpl extends ServiceImpl<WebconMapper, Webcon> implements IWebconService {

    @Autowired
    private WebconMapper webconMapper;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ISolutionService solutionService;

    /**
     * 添加或者修改分控信息
     * @param webcon
     * @return
     */
    @Override
    @Transactional
    public int insertOrUpdate(Webcon webcon) {

        //根据主键查询分控
        Webcon webc = webconMapper.selectById(webcon.getAccount());
        if(webc == null){
            //创建该分控的默认方案
            solutionService.createDefaultSolution(webcon);
            //添加分控
            return webconMapper.insert(webcon);
        } else {
            return webconMapper.updateById(webcon);
        }
    }

    /**
     * 批量删除分控
     * @param mid
     * @return
     */
    @Override
    @Transactional
    public int deleteByIds(String[] mid) {
        System.out.println("需要删除的分控：" + Arrays.toString(mid));

        //批量删除所有分控
        webconMapper.deleteBatchIds(Arrays.asList(mid));

        for (String account : mid) {
            //根据删除的分控，删除该分控下的所有任务
            taskService.deleteByAccount(account);

            //根据分控删除所有方案
            solutionService.deleteByAccount(account);
        }
        return 1;
    }

    /**
     * 移除终端和分控之间的关系
     * @param webcon
     * @param userid
     * @return
     */
    @Override
    @Transactional
    public int deleteClientUsers(Webcon webcon, Integer userid) {

        //获得分控下所有组终端关系
        List<String[]> groupList = parseGroups(webcon);

        //迭代循环
        Iterator<String[]> iterator = groupList.iterator();
        while (iterator.hasNext()){
            String[] groupUsers = iterator.next();
            //判断终端是否删除
            if (Integer.parseInt(groupUsers[1]) == userid){
                //移除分控与该终端的关系
                iterator.remove();
            }
        }

        //重新生成终端关系
        webcon.setClients(createGroups(groupList));

        //更新终端关系
        webconMapper.updateById(webcon);

        //删除只有这个终端的相关任务
        taskService.deleteTaskByUid(userid, webcon);

        return 1;
    }


    /**
     * 解析分控下的所有终端组
     * @return
     */
    private List<String[]> parseGroups(Webcon webcon){
        List<String[]> groupsList = new ArrayList<>();
        //获得当前分控下的所有终端
        String clients = webcon.getClients();
        String[] groups = clients.split("\\|");
        for (String group : groups) {
            groupsList.add(group.split("-"));
        }
        return groupsList;
    }

    private String createGroups(List<String[]> groups){
        StringBuffer stringBuffer = new StringBuffer();

        for (String[] group : groups) {
            if(stringBuffer.length() > 0){
                stringBuffer.append("|");
            }

            stringBuffer.append(group[0]).append("-").append(group[1]);
        }

        return stringBuffer.toString();
    }
}
